package com.example.diffservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.common.constant.Constants;
import com.example.diffservice.algorithm.util.FileUtil;
import com.example.diffservice.entity.*;
import lombok.AllArgsConstructor;

import java.io.File;
import java.io.FileFilter;
import java.util.List;

/**
 * @author DestinyCc
 * @date 2022/4/6
 */
@AllArgsConstructor
public class ResultGenerator {
    String commitId;

    String author;

    String pullNumber;

    String projectName;

    public DiffResult generate(){
        DiffResult diffResult = new DiffResult();
        //D:\output\jruby\7164\kares\c6b80b4189f77e2d7ba6c8fa556f554e64a11ba9\gen
        String rootPath = Constants.DEFAULT_OUTPUT_DIR
                + projectName + "/"
                + pullNumber + "/"
                + author + "/"
                + commitId + "/";
        File file = new File(rootPath + "gen/");
        String[] commitIds = file.list();
        for (String commitId: commitIds){
            File dir = new File(rootPath + "gen/" + commitId);
            File[] jsonFiles = dir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    if (pathname.isDirectory()){
                        return false;
                    }
                    return true;
                }
            });
            SingleFileDifference singleFileDifference;
            for (File json: jsonFiles){
                JSONArray array = JSON.parseArray(FileUtil.read(json.getAbsolutePath()));
                int size = array.size();
                String filename = json.getName().split("\\.")[0].substring(4).replace("_", "/") + ".java";
                singleFileDifference = new SingleFileDifference(filename);
                for (int i = 0; i < size; i++) {
                    JSONObject diff = array.getJSONObject(i);
                    String id = diff.getString("id");
                    StringBuilder description = new StringBuilder(diff.getString("description"));
                    if (diff.containsKey("opt2-exp2")){
                        List<String> op2_exp2 = diff.getObject("opt2-exp2", List.class);
                        for (String str: op2_exp2){
                            description.append("\n").append(str);
                        }
                    }
                    String range = diff.getString("range");
                    if (range.startsWith("-")){
                        range = range.substring(1);
                    }
                    singleFileDifference.addDifference(new Difference(id, description.toString(), range));
                }
                diffResult.addSingleFileDifference(singleFileDifference);
            }
        }
        JSONObject linkObj = JSON.parseObject(FileUtil.read(rootPath + "Link.json"));
        JSONArray linkArray = linkObj.getObject("links", JSONArray.class);
        for (int i = 0; i < linkArray.size(); i++) {
            JSONObject obj = linkArray.getJSONObject(i);
            if (obj.getObject("links", JSONArray.class).size() != 0){
                JSONArray links = obj.getObject("links", JSONArray.class);
                for (int j = 0; j < links.size(); j++) {
                    JSONObject link = links.getJSONObject(j);
                    diffResult.addLink(new Link(link.getString("from"), link.getString("to"), link.getString("desc")));
                }
            }
        }
        return diffResult;
    }
}
